package com.frossini.proyectologin;

import java.util.ArrayList;

import com.frossini.proyectologin.adapter.ClientesAdapter;
import com.frossini.proyectologin.adapter.TareasAdapter;
import com.frossini.proyectologin.model.Cliente;
import com.frossini.proyectologin.model.ExtraKeys;
import com.frossini.proyectologin.model.Tarea;
import com.frossini.proyectologin.util.ClienteSerializer;
import com.frossini.proyectologin.util.TareaSerializer;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class BusquedaTareasActivity extends ActionBarActivity 
{
	private TareasAdapter adapter;
	private ListView lista ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_busqueda_tareas);
		
		adapter = new TareasAdapter(this);
		lista = (ListView) findViewById(R.id.lvbusq_tareas);
		lista.setAdapter(adapter);
		lista.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
			{
				Tarea c = (Tarea) arg0.getItemAtPosition(arg2);

				Intent returnIntent = new Intent();
				returnIntent.putExtra(ExtraKeys.S_TAREA,c);
				setResult(RESULT_OK,returnIntent);
				finish();
				
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		
			MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.busqueda_tareas, menu);
	        SearchManager SManager =  (SearchManager) getSystemService(Context.SEARCH_SERVICE);
	        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
	        SearchView searchViewAction = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
	        searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName()));
	        searchViewAction.setIconifiedByDefault(false);
            searchViewAction.setOnQueryTextListener(new OnQueryTextListener() {
				
				@Override
				public boolean onQueryTextSubmit(String arg0) {
					actualizaradapter(arg0);
					return false;
				}
				
				@Override
				public boolean onQueryTextChange(String arg0) {
					actualizaradapter(arg0);
					return false;
				}
			});
	        
	        return super.onCreateOptionsMenu(menu);
    
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	private void actualizaradapter(String sbusqueda)
	{
		
		ArrayList<Tarea> listtareas = TareaSerializer.busquedatarea(sbusqueda, this);
		// Limpiar el adapter
		adapter.clear();
		// y agregar la lista clientes al adapter
		
		// addall funciona en android 4 para arriba en versiones anteriores 
		// se usa un for 
		adapter.addAll(listtareas);
		//lista = (ListView) findViewById(R.id.lvbusq_clientes);
		
		
	 }

	// on contextItem seleced es cuando tenmes presinado el item y mostras un menu
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		return super.onContextItemSelected(item);
	}


}
